var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
var checkLogin =require('../middleware/checkLogin');
var mock=require('../models/mock');
module.exports = router;

//博客详细
router.get('/:name/:_id', function (req,res) {
  Post.getById(req.params._id, function (err, post) {
    if(err || !post){
      return res.render('error',{message:'查询博客失败',error:err});
    }

    res.render('article',{
      title:'博客详细页',
      post:post,
      user:req.session.user,
      success:req.flash('success').toString(),
      error:req.flash('error').toString(),
      back:req.url
    });
  });
});

//保存评论
router.post('/:name/:_id',checkLogin, function (req,res) {
  var user=req.session.user;
  var comment={
    user:{_id:user._id,name:user.name,face:user.face,email:user.email},
    content:req.body.content,
    time:mock.dateFormat(new Date(),'all',true)
  };
  Post.addComment(req.params._id,comment,function (err) {
    if(err){
      req.flash('error','评论失败');
    }else{
      req.flash('success','评论成功');
    }
    return res.redirect('back');
  });
});